//
//  UIButton+LFButton.m
//  XiangMuXieTong
//
//  Created by lifushuai on 16/5/5.
//  Copyright © 2016年 jiankeyan. All rights reserved.
//

#import "UIButton+LFButton.h"

@implementation UIButton (LFButton)
/** button 的背景图片 */
+ (UIButton *)buttonWithBackgroundImageName:(NSString *)imageName{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    return button;
}
/** button 的背景图片 */
+ (UIButton *)buttonWithBackgroundImageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName{
    UIButton *button = [UIButton buttonWithBackgroundImageName:imageName];
    [button setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    return button;
}
/** button 的背景图片 和 点击后的背景图片 */
+ (UIButton *)buttonWithBackgroundImageName:(NSString *)imageName highlightedImageName:(NSString *)selectedImageName{
    UIButton *button = [UIButton buttonWithBackgroundImageName:imageName];
    [button setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];
    return button;
}




/** button 只有文字大小 默认居中*/
+ (UIButton *)buttonWithTitle:(NSString *)title setTitleColor:(UIColor *)color setTitleFont:(UIFont *)font setState:(UIControlState)state{
    UIButton *button = [UIButton buttonWithTitle:title setTitleFont:font];
    [button setTitleColor:color forState:state];
    [button sizeToFit];
    return button;
}
/** button 的图片 和 点击后的图片 */
+ (UIButton *)buttonWithImageName:(NSString *)imageName highlightedImageName:(NSString *)selectedImageName titleFont:(UIFont *)font{
    UIButton *button = [UIButton buttonWithTitle:@"" setTitleFont:font];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateSelected];
    return button;
}
/** 可以设置button的文字排列方向 */
+ (UIButton *)buttonWithTitle:(NSString *)title setTitleColor:(UIColor *)color setTitleFont:(UIFont *)font setTextAlignment:(UIControlContentHorizontalAlignment)alignment{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    button.titleLabel.font = font;
    button.contentHorizontalAlignment = alignment;
    return button;
}
/** button 只有文字大小 默认文字黑色 默认居中*/
+ (UIButton *)buttonWithTitle:(NSString *)title setTitleFont:(UIFont *)font{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = font;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    return button;
}
/** 可以设置button的文字大小颜色排列方向 背景图片 和 选中后的背景图片*/
+ (UIButton *)buttonWithTitle:(NSString *)title setTitleColor:(UIColor *)color setTitleFont:(UIFont *)font setTextAlignment:(UIControlContentHorizontalAlignment)alignment setBackgroundImageName:(NSString *)imageName highlightedImageName:(NSString *)selectedImageName{
    UIButton *button = [UIButton buttonWithTitle:title setTitleColor:color setTitleFont:font setTextAlignment:alignment];
    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateHighlighted];
    return button;
    
}

/** 可以设置button的文字大小颜色排列方向 图片 和 选中后的图片*/
+ (UIButton *)buttonWithTitle:(NSString *)title setTitleFont:(UIFont *)font setNormalImageName:(NSString *)imageName andSelectImageName:(NSString *)selectImageName{
    UIButton *button = [UIButton buttonWithTitle:title setTitleFont:font];
    [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:selectImageName] forState:UIControlStateSelected];
    return button;
}
/**
 *  设置button的背景颜色和状态的颜色
 */
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state{
    [self setBackgroundImage:[UIButton imageWithColor:backgroundColor] forState:state];
}
+ (UIImage *)imageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
    
}

@end
